package com.mk.cloud.common.util;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Map;

@SuppressWarnings("unused")
public class MapUtils extends org.apache.commons.collections.MapUtils {
    /**
     * 支持key {emplooy:{name:"cc"}}  emplooy.name
     *
     * @param m
     * @param key
     * @param defaultValue
     * @return
     */
    public static Object get(Map m, String key, String defaultValue) {
        String[] prem = key.split("\\.");
        Object o = null;
        for (String keyElement : prem) {
            o = m.get(keyElement);
            if (o instanceof Map)
                m = (Map) o;
            else break;
        }
        return o == null ? defaultValue : o;
    }

    public static <T> T getT(Map map, String key, T c) {
        if (map.containsKey(key)) {
            return (T) map.get(key);
        }
        return null;
    }

    public static BigDecimal getBigDecimal(Map map, String key, BigDecimal defaultValue) {
        if (map.containsKey(key)) {
            if (StringUtils.isNotBlank(MapUtils.getString(map, key))) {
                return new BigDecimal(MapUtils.getString(map, key));
            } else {
                return defaultValue;
            }
        }
        return defaultValue;
    }
}
